<?php
class shopControl extends skymvc{
	public $shopid;
	public function __construct(){
		parent::__construct();	 
	}
	
	public function onInit(){
		$shop=M("login")->getShopAdmin();
	}
	
	public function onDefault(){
		$shopid=SHOPID;
		
		if($shopid){
			$data=M("shop")->selectRow(array("where"=>"shopid={$shopid}"));
			
			$data['content']=M("shop_data")->selectOne(array("where"=>"shopid=".$shopid,"fields"=>" content"));	
		}
		 
		$cat_list=M("category")->children(0,MODEL_SHOP_ID);
		$this->smarty->assign(array(
			"data"=>$data,
			 
			 
			"cat_list"=>$cat_list
			
		));
		$this->smarty->display("shop/add.html");
	}
	
 
	
	public function onPlugin(){
		$shopid=SHOPID;
		
		if($shopid){
			$data=M("shop")->selectRow(array("where"=>"shopid={$shopid}"));
			
			$this->smarty->assign(array(
				"data"=>$data
			));
		}
		$this->smarty->display("shop/plugin.html");
	}
	
	public function onPluginSave(){
		$shopid=SHOPID;
		$data=M("shop")->postData(); 
		M("shop")->update($data,"shopid='$shopid'");
		$this->goall("保存成功");
	}
	
	
	public function onSave(){
		
		$shopid=SHOPID;
		$data=M("shop")->postData(); 
		 
		$data["sc_id"]=array_lastnum(get_post("sc_id","i"));
		 
		$data["send_startprice"]=round(get_post("send_startprice"),1);
		 
		//gps信息
		if(post('latlng')){
			$latlng=explode(",",post('latlng'));
			$data['lat']=$latlng[0];
			$data['lng']=$latlng[1];
		}elseif(post('lat')){
			$data['lat']=post('lat');
			$data['lng']=post('lng');
		}
		if($shopid){
			
			M("shop")->update($data,"shopid='$shopid'");
			M("shop_data")->update(array("content"=>post('content','x')),"shopid=".$shopid);
			if(!M("shop_data")->selectRow("shopid=".$shopid)){
				M("shop_data")->insert(array("shopid"=>$shopid,"content"=>post('content','x'),"dateline"=>time()),"shopid=".$shopid);
			}
		}else{
			$data["dateline"]=time();
			M("shop")->insert($data);
			M("shop_data")->insert(array("shopid"=>$shopid,"content"=>post('content','x'),"dateline"=>time()),"shopid=".$shopid);
		}
		$this->goall("保存成功");
	}
	
}
?>